<?php
namespace app\admin\library;

use cubox\Cubox;

class User{

    private static string $userSessionKey = 'admin_user';
    public static string $usernameReg = '/^[a-zA-Z]{1}[a-zA-Z0-9_]{3,15}$/';
    

    public static  function isLogin(){

        if (isset($_SESSION[self::$userSessionKey])){
            return true;
        }

        return false;
    }

    public static function logout(){
        unset($_SESSION[self::$userSessionKey]);
    }

    public static function setLoginData(array $data){
        unset($data['password']);
        $_SESSION[self::$userSessionKey] = $data;
    }


    public static function super():bool{
        return self::get('power')=='ALL';
    }

    public  static function makePassword(string $password){
        //加盐
        $salt  = 'Cubox';
        return md5($salt.$password);
    }

    public static function get($key){
        if (isset($_SESSION[self::$userSessionKey]) && isset($_SESSION[self::$userSessionKey][$key]) ){
            return $_SESSION[self::$userSessionKey][$key];
        }else{
            return null;
        }
    }

    public static function getPowerExtend($app,$power,$name,$default=null){
        $extend = self::get('power_extend');

        if (isset($extend[$power]) &&  isset($extend[$power][$app]) &&  isset($extend[$power][$app][$name])){
            return $extend[$power][$app][$name];
        }else{
            return $default;
        }
    }

    public static function set($key,$value){
        if (isset($_SESSION[self::$userSessionKey])){
            $_SESSION[self::$userSessionKey][$key] = $value;
        }
    }

    public static function del($key){
        if (isset($_SESSION[self::$userSessionKey])){
            unset($_SESSION[self::$userSessionKey][$key]);
        }
    }


    public static function getFrontData():array{

      if (isset($_SESSION[self::$userSessionKey])){
        $data = $_SESSION[self::$userSessionKey];
          
        $re ['user_id'] =  $data['user_id'];
        $re ['nickname'] =  $data['nickname'];
        $re ['username'] =  $data['username'];
        $re ['power'] =  $data['power'];
        //$re ['power_extend'] =  $data['power_extend'];
        $re ['group_id'] =  $data['group_id'];
        $re ['status'] =  $data['status'];
        $re ['photo'] =  $data['photo'];
        $re ['group_title'] = $data['group_title'];
          
        return $re;
      }

      return [];
    }
    public static function power($power):bool{
        $powerStr = self::get('power');
        
        if ($powerStr == 'ALL') return true;
        $len  = strlen($power);
        if ($powerStr){
            $list = explode(',', $powerStr);
            
            foreach ($list as $key => $value) {
                if (substr($value, 0, $len) == $power) {
                    return true;
                }
            }
        }

        return false;
    }

    public static function app():array{

        if (self::get('app')!=null) return self::get('app');

        $appInfoList =  Cubox::getAppList();
        $list = array_keys($appInfoList);

        foreach($list as $key=>$app){
            if (!self::power($app)){
                unset($list[$key]);
            }
        }

        self::set('app',$list);
        return $list;
    }

    public static  function mergeExtend($parent,$child){

        if ($parent==null || $parent=='') return $child;
        if ($child==null || $child=='') return $parent;
        
        if (is_array($parent) && is_array($child)){

            foreach($parent as $key=>$val){

                if (is_array($val)){
                    if (isset($child[$key])){
                        $parent[$key] = self::mergeExtend($val,$child[$key]);
                    }
                }else{
                    if (isset($child[$key]) && $child[$key]!==''){
                        $parent[$key] = $child[$key];
                    }
                }
            }
        }

        return $parent;
    }
}